import unittest

import requests
from parameterized import parameterized

from api.open_account_api import OpenAccountApi
from api.reg_login_api import RegLoginApi
from common.util import common_assert, read_json, html_parser, build_account
from config import path_cfg


class TestOpenAccount(unittest.TestCase):

    def setUp(self) -> None:
        self.session = requests.Session()
        self.reg_login_api = RegLoginApi(self.session)
        self.open_account_api = OpenAccountApi(self.session)

        # 注册用户
        phone = path_cfg.REG_LOGIN_PHONE
        build_account('open_user', self.session, phone)
        self.reg_login_api.user_login({"keywords": phone, "password": "a123456"})

    def tearDown(self) -> None:
        self.session.close()

    # 实名认证测试用例
    @parameterized.expand(read_json('open_account_data.json', 'real_name'))
    def test01_real_name(self, real_name_body, respect):
        resp = self.open_account_api.real_name(real_name_body)

        common_assert(respect, resp)

    # 开户测试用例
    def test02_open_account(self):
        resp = self.open_account_api.open_account()

        self.assertEqual(200, resp.status_code)
        self.assertEqual(200, resp.json().get('status'))

    # 第三方开户测试用例
    def test03_third_open_account(self):
        resp = self.open_account_api.open_account()

        form_data = resp.json().get('description').get('form')

        parser_data = html_parser(form_data)

        resp = self.open_account_api.third_open_account(parser_data.get('url'), parser_data.get('req_body'))
        self.assertIn('OK', resp.text)

